﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.Behaviors
{
  public class DataGridSelectionChangedCommandBehaviour : CommandBehaviorBase<DataGrid>
  {
    public DataGridSelectionChangedCommandBehaviour(DataGrid targetObject)
      : base(targetObject)
    {
      targetObject.SelectionChanged += this.OnSelectionChanged;
    }
    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      this.ExecuteCommand();
    }
  }

  public static class SelectionChanged
  {
    /// <summary>
    /// Command paramter.
    /// </summary>
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached(
            "CommandParameter", typeof(object), typeof(SelectionChanged), new PropertyMetadata(OnSetCommandParameterCallback));

    /// <summary>
    /// Command to execute on click event.
    /// </summary>
    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
        "Command", typeof(ICommand), typeof(SelectionChanged), new PropertyMetadata(OnSetCommandCallback));

    /// <summary>
    ///  Command behavior.
    /// </summary>
    private static readonly DependencyProperty ClickCommandBehaviorProperty =
        DependencyProperty.RegisterAttached(
            "ClickCommandBehavior", typeof(DataGridSelectionChangedCommandBehaviour), typeof(SelectionChanged), null);


    /// <summary>
    /// Gets command
    /// </summary>
    /// <param name="dataGrid">
    /// containig dependecy property
    /// </param>
    /// <returns>
    /// attached command
    /// </returns>

    public static ICommand GetCommand(DataGrid dataGrid)
    {
      return dataGrid.GetValue(CommandProperty) as ICommand;
    }

    /// <summary>
    /// Gets command parameter
    /// </summary>
    /// <param name="dataGrid">
    /// containig dependecy property
    /// </param>
    /// <returns>

    /// attached command

    /// </returns>

    public static object GetCommandParameter(DataGrid dataGrid)
    {
      return dataGrid.GetValue(CommandParameterProperty) as object;
    }


    /// <summary>
    /// Sets the <see cref="ICommand"></see> for execute
    /// </summary>
    /// <param name="dataGrid">
    /// dependency object to attache command
    /// </param>

    /// <param name="command">

    /// command for execute

    /// </param>

    public static void SetCommand(DataGrid dataGrid, ICommand command)
    {
      dataGrid.SetValue(CommandProperty, command);
    }

    /// <summary>
    /// Sets the value for the CommandParameter attached property on the provided <see cref="DataGrid"/>.
    /// </summary>
    /// <param name="dataGrid">

    /// dependency object to attach CommandParameter

    /// </param>

    /// <param name="parameter">

    /// Parameter value to attach

    /// </param>

    public static void SetCommandParameter(DataGrid dataGrid, object parameter)
    {
      dataGrid.SetValue(CommandParameterProperty, parameter);
    }



    /// <summary>

    /// Get or create command behavior

    /// </summary>

    /// <param name="dataGrid">

    /// dependecy object to attach

    /// </param>

    /// <returns>

    /// click behavior

    /// </returns>

    private static DataGridSelectionChangedCommandBehaviour GetOrCreateBehavior(DataGrid dataGrid)
    {

      var behavior = dataGrid.GetValue(ClickCommandBehaviorProperty) as DataGridSelectionChangedCommandBehaviour;

      if (behavior == null)
      {

        behavior = new DataGridSelectionChangedCommandBehaviour(dataGrid);

        dataGrid.SetValue(ClickCommandBehaviorProperty, behavior);

      }



      return behavior;

    }



    /// <summary>

    /// On set command

    /// </summary>

    /// <param name="dependencyObject">

    /// dependecy object to attach command

    /// </param>

    /// <param name="e">

    /// event args

    /// </param>

    private static void OnSetCommandCallback(

        DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {

      var dataGrid = dependencyObject as DataGrid;

      if (dataGrid != null)
      {

        DataGridSelectionChangedCommandBehaviour behaviour = GetOrCreateBehavior(dataGrid);

        behaviour.Command = e.NewValue as ICommand;

      }

    }



    /// <summary>

    /// On set command parameter.

    /// </summary>

    /// <param name="dependencyObject">

    /// dependency object to attach command

    /// </param>

    /// <param name="e">

    /// event args

    /// </param>

    private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {

      var dataGrid = dependencyObject as DataGrid;

      if (dataGrid != null)
      {

        DataGridSelectionChangedCommandBehaviour behavior = GetOrCreateBehavior(dataGrid);

        behavior.CommandParameter = e.NewValue;

      }

    }

  }


}
